TC_TYPE_CONFIG = {
    TYPE:'',
    BINDING_URI: base_url + 'cms/cp/type/bindingbytype/',
    EDITPANEL_URI:base_url + 'cms/cp/type/editpanel/',
    COMMIT_URI:base_url + 'cms/cp/type/oncommit/',
    DELETE_URI:base_url + 'cms/cp/type/ondelete/'
};
var TC_TYPE = (function() {
	
    var oTable;
    var that = this;
    return {
        createtable :   function(){
            oTable = $('#data_table').dataTable({
                aaSorting: [],
                aoColumns: [
                {
                    mData: "type_id",
                    sWidth: "40px",
                    sClass:'gridAction',
                    mRender: function ( value, type, datarow ) {
                        var str = '';
                        str += '<ul class="table-controls">';
                        //str += '<li><a href="#" class="tip" onclick="" title="Add entry" ><i class="icon-plus"></i></a> </li>'+
                        str += 	'<li><a href="#" class="tip" onclick="TC_TYPE.onEditItem(' + value + ')" title="Edit entry" ><i class="icon-pencil"></i></a> </li>';
                        if(	datarow.type_lock!=1)
                            str += 	'<li><a href="#" class="tip" onclick="TC_TYPE.onDeleteItem(' + value + ')" title="Remove entry" ><i class="icon-trash"></i></a> </li>';
                        str += '</ul>';
                        return str;
                            
                    }
                },
                {
                    mData: "type_title"
                },
                {
                    mData: "type_position",
                    sWidth: "60px"
                },
                {
                    mData: "type_insert",
                    sWidth: "120px"
                },

                {
                    mData: "type_update",
                    sWidth: "120px"
                },

                {
                    mData: "type_lock",
                    sWidth: "40px",
                    bVisible:false
                }
                ],
                sServerMethod: "POST",
                iDisplayLength: 50,
                bProcessing: true,
                bServerSide: true,
                bPaginate: false,
                sAjaxSource: TC_TYPE_CONFIG.BINDING_URI+TC_TYPE_CONFIG.TYPE,
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    sSearch: "<span>Filter records:</span> _INPUT_",
                    sLengthMenu: "<span>Show entries:</span> _MENU_",
                    oPaginate: {
                        "sFirst": "First", 
                        "sLast": "Last", 
                        "sNext": ">", 
                        "sPrevious": "<"
                    }
                }
            });
            oTable.fnSetFilteringDelay(2000);
        },
        onInit  :   function(){
            this.createtable();
        },
        onAddItem:function(){
            this.onEditItem(0);
        },
        onEditItem:function(Id){
            if (PENDING)
                return false;
            $('#dialog-edit-panel>.base-dialog-modal').html('<div class="base_loading" style="display: block;height: 60px"></div>');
            bckdialog({
                title:'Add/Edit Type',
                message:$('#dialog-edit-panel'),
                buttons: [
                {
                    text    :'Done',
                    class   :'base_button base_green base-small-border-radius',
                    click   : function() {
                        TC_TYPE.onSave();
                    }
                },
                {
                    text    :'Close',
                    class   :'base_button base_red base-small-border-radius',
                    click   : function() {
                        $(this).dialog("close");
                    }
                }
                ]
            }).open();
            backend({
                url         :   TC_TYPE_CONFIG.EDITPANEL_URI+TC_TYPE_CONFIG.TYPE,
                data        :   {
                    Id  :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        $('#dialog-edit-panel>.base-dialog-modal').html('<span style="color:red">'+rsdata.message+'</span>');
                    }else{
                        $('#dialog-edit-panel>.base-dialog-modal').html(rsdata.htmlreponse);
                        $('#dialog-edit-panel').dialog('close').dialog('open'); 
                        $('#_form_edit_panel select').select2({
                            minimumResultsForSearch :-1
                        });
                    }
                }
            }).call();
        },
        onSave:function(){
            if( $('#_form_edit_panel').validationEngine('validate') === false)return false;
            var Params=$('#_form_edit_panel').serializeObject();
            $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').show();
            new backend({
                url         :   TC_TYPE_CONFIG.COMMIT_URI,
                data        :   {
                    Params  :   Params,
                    Id      :   $('#type_id').val()
                },
                callback    :   function(rsdata){
                    $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').hide();
                    if(rsdata.result<0){
                        bckdialog({
                            type:'error'
                        }).open(rsdata.message);
                    }else{
                        TC_TYPE.onRefresh();
                        TC_TYPE.onCancel();
                    }
                    
                }
            }).call();
            return false;
        },
        onCancel:function(){
            $('#dialog-edit-panel').dialog('close');
        },
        onDeleteItem:function(Id){
            backend_delete(TC_TYPE_CONFIG.DELETE_URI,Id,function(){
                TC_TYPE.onRefresh();
            });
        },
        onRefresh:function(){
            oTable.fnDraw(false);
        },
        onFillter:function(v){
            oTable.fnFilter(v, 2);
        },
        onStatusChange:function(that,Id){
            var _status = $(that).is(':checked')?'true':'false';
            new backend({
                url         :   TC_TYPE_CONFIG.COMMIT_URI,
                data        :   {
                    Params  :   {
                        staf_status:_status
                    },
                    Id      :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        bckdialog({
                            type:'error'
                        }).open(rsdata.message);
                    }
                }
            }).call();
        },
        onChangeSource : function(type){
            if(type!==TC_TYPE_CONFIG.TYPE){
                TC_TYPE_CONFIG.TYPE=type;
                oTable.fnSettings().sAjaxSource = (TC_TYPE_CONFIG.BINDING_URI+TC_TYPE_CONFIG.TYPE);
                oTable.fnDraw(false);
            }
        }
    };
}());